package app;

import android.app.Application;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;

import com.example.zx.theline.MainActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.mingle.widget.ShapeLoadingDialog;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import Utils.ActivityManager;
import Utils.DictionariesUtil;
import dialog.WaitDialog;
import greendao.MySQLiteOpenHelper;
import greendao.UnitInformationDao;
import greendao.UserDao;
import greendao.entity.ProtectMeasures;
import greendao.entity.SecurityRisk;
import greendao.entity.UnitInformation;
import greendao.entity.User;
import network.NetWork;
import user.LoginUser;
import greendao.DaoMaster;
import greendao.DaoSession;

/**
 * Created by 任科全 on 2017/3/13.
 */

public class MyApplication extends Application {
    static MyApplication mMyApplication;
    private static LoginUser mLoginUser;

//    DaoMaster.DevOpenHelper devOpenHelper;
    DaoMaster mDaoMaster;
    static DaoSession mDaoSession;

    static ShapeLoadingDialog mWaitDialog;

    public final static int APP_STATUS_KILLED = 0; // 表示应用是被杀死后在启动的
    public final static int APP_STATUS_NORMAL = 1; // 表示应用时正常的启动流程

    public static int APP_STATUS = APP_STATUS_KILLED; // 记录App的启动状态


    @Override
    public void onCreate() {
        super.onCreate();
        Init();
        InitDB();
    }


    public void Init() {
        mLoginUser = new LoginUser();
        mMyApplication = this;
    }

    public void InitDB() {
//        devOpenHelper = new DaoMaster.DevOpenHelper(MyApplication.getInstance(), "TheLine", null);
//        mDaoMaster = new DaoMaster(devOpenHelper.getWritableDb());
        MySQLiteOpenHelper devOpenHelper = new MySQLiteOpenHelper(MyApplication.getInstance(), "TheLine.db", null);
        mDaoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
        mDaoSession = mDaoMaster.newSession();
    }

    public static void ShowWaitDialog(Context context) {
        if (mWaitDialog != null && mWaitDialog.isShowing()) {
            mWaitDialog.dismiss();
        }
        mWaitDialog = new ShapeLoadingDialog.Builder(context).loadText("正在加载数据，请稍等！").build();
        if (!mWaitDialog.isShowing())
            mWaitDialog.show();
    }

    public static void setWaitDialogMsg(String s) {
        if (mWaitDialog.isShowing()) {
            mWaitDialog.setLoadingText(s);
        }

    }

    public static Dialog getWatiDialog() {
        return mWaitDialog;
    }

    public static void HideWaitDialog() {

//        showNetError();

        if (mWaitDialog.isShowing()) {
            mWaitDialog.dismiss();
        }
    }

    //dialog提示网络错误
    public static void showNetError(){
        mWaitDialog.showConfirmBtn();
        mWaitDialog.setLoadingText("网络不通畅，请重试一下哦~~");
        mWaitDialog.hideImage();
        //网络错误点击外部提示框让dialog消失
        mWaitDialog.setCanceledOnTouchOutside(true);
    }


    public static MyApplication getInstance() {
        return mMyApplication;
    }

    public static LoginUser getmLoginUser() {
        return mLoginUser;
    }

    public static void setmLoginUser(LoginUser mLoginUser) {
        MyApplication.mLoginUser = mLoginUser;
    }

    public static DaoSession getDaoSession() {
        return mDaoSession;
    }

    /**
     * 重新初始化应用界面，清空当前Activity棧，并启动欢迎页面
     */
    public static void reInitApp() {
        Intent intent = new Intent(getInstance(), MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        getInstance().startActivity(intent);
    }



}
